package com.jzg.sys.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jzg.sys.common.rest.BaseRest;
import com.jzg.sys.common.vo.ResultVO;
import com.jzg.sys.dao.entity.SecRole;
import com.jzg.sys.dao.entity.SecUser;
import com.jzg.sys.dao.entity.SecUserRole;
import com.jzg.sys.manager.SecRoleManager;
import com.jzg.sys.manager.SecUserRoleManager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping(value = "/roleUserAction")
public class RoleUserAction extends BaseRest {
	@Autowired
	private SecUserRoleManager userRoleManager;

	@RequestMapping(value = "/findNewUserPageWithRoleId")
	@ResponseBody
	public IPage<SecUser> findUserPageWithRoleId(Page<SecUser> easyPage, String roleId,SecUser user){
		if(StringUtils.isBlank(roleId)){
			return null;
		}else{
			return userRoleManager.findNewUserPageWithRoleId(easyPage,roleId,user);
		}
	}

	@RequestMapping(value = "/deleteUser")
	@ResponseBody
	public ResultVO deleteUser(String roleId,String userId) {
		ResultVO vo = new ResultVO();
		try {
			userRoleManager.deleteUser(roleId,userId);
		} catch (RuntimeException be) {
			be.printStackTrace();
			vo.setResult(false);
			vo.setMsg(be.getMessage());
			return vo;
		}
		return vo;
	}

	@RequestMapping(value = "/findUserPageExceptRoleId")
	@ResponseBody
	public IPage<SecUser> findUserPageExceptRoleId(Page<SecUser> page,String roleId,SecUser user,String sysCode){
		user.setSysCode(sysCode);
		return userRoleManager.findUserPageExceptRoleId(page,roleId,user);
	}

	@RequestMapping(value = "/saveRoleUser")
	@ResponseBody
	public ResultVO saveRoleUser(String json) {
		ResultVO obj = new ResultVO();
		try {
			JSONObject jsonObj = JSONObject.parseObject(json);
			JSONArray secUsers = jsonObj.getJSONArray("secUsers");
			String roleId = jsonObj.getString("roleId");
			userRoleManager.saveRoleUser(roleId,secUsers);
			return obj;
		} catch (Exception e) {
			obj.setResult(false);
			obj.setMsg(e.getMessage());
			return obj;
		}
	}
}
